package com.muqi.app.qlearn.mine;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.easemob.chat.EMChatManager;
import com.easemob.easeui.widget.CircularImage;
import com.muqi.app.network.http.GlideImageUtils;
import com.muqi.app.project.utils.CustomerUtil;
import com.muqi.app.project.utils.QrCodeUtils;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.modles.UserInfo;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;

public class QrCodeCardActivity extends BaseFragmentActivity {
	private TextView nameTv;
	private TextView infoTv;
	private CircularImage avatarImg;
	private ImageView qrCodeImg;
	protected TextView descTv;
	private UserInfo userinfo = null;
	private Bitmap qrbitmap = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aty_qrcode_show);
	}

	@Override
	protected void onInit() {
		nameTv = (TextView)findViewById(R.id.qrName);
		infoTv = (TextView)findViewById(R.id.qrInfo);
		descTv = (TextView)findViewById(R.id.qrDesc);
		avatarImg = (CircularImage)findViewById(R.id.qrCircularImage);
		qrCodeImg = (ImageView)findViewById(R.id.qrCodeImg);
	}

	@Override
	protected void onData() {
		userinfo = CustomerUtil.getUserInfo(this);
		if(userinfo == null){
			finish();
			return;
		}
		
		String qrCode = QrCodeUtils.buildQrCode("add_friend", EMChatManager.getInstance().getCurrentUser(), "", "");
		qrbitmap = CustomerUtil.createQRImage(qrCode, 300, 300);
		if(qrbitmap == null){
			ShowToast.showShort(this, "未生成名片信息");
			finish();
		}else{
			qrCodeImg.setImageBitmap(qrbitmap);
		}
		
		if(userinfo.avatarInfo!=null){
			GlideImageUtils.setAvatar(this, userinfo.avatarInfo.fileurl_01, avatarImg);
		}
		if(userinfo.name!=null){
			nameTv.setText(userinfo.name);
		}
		if(userinfo.mobile!=null){
			infoTv.setText(userinfo.mobile);
		}else{
			infoTv.setText("");
		}
	}
	
	@Override
	protected void onDestroy() {
		if(qrbitmap!=null){
			qrbitmap.recycle();
		}
		super.onDestroy();
	}
	
	public void onBack(View v){
		finish();
	}
}
